//
//  ViewController.swift
//  UITableViewDemo
//
//  Created by chengxiang on 2018/6/24.
//  Copyright © 2018年 chengxiang. All rights reserved.
//

import UIKit

class ThirdSubViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
    
    let dataSource = ["item1","item2","item3","item4","item5","item6","item7","item8","item9","item10","item1","item2","item3","item1","item2","item3","item1","item2","item3","item1","item2","item3",]
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.dataSource.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let identified = "result cell"
        var cell = tableView.dequeueReusableCell(withIdentifier: identified)
        if(cell == nil){
            cell = UITableViewCell(style: .default, reuseIdentifier: identified)
        }
        let rowNum = indexPath.row
        cell?.textLabel?.text = self.dataSource[rowNum]
        cell?.detailTextLabel?.text = "detail information here!"
        let _defaultImg = UIImage(named: "timg")
        let _selectedImg = UIImage(named: "timg-2")
        cell?.imageView?.image = _defaultImg
        cell?.imageView?.highlightedImage = _selectedImg
        cell?.setHighlighted(true, animated: true)
        let view = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
        view.backgroundColor = UIColor.brown
        if(rowNum%2==0){
            cell?.backgroundView = view
        }
        return cell!
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 50
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        let boundle = UIScreen.main.bounds
        // Do any additional setup after loading the view, typically from a nib.
        let _frame = CGRect(x: 0, y: 40, width: boundle.width, height: boundle.height)
        let tableView = UITableView(frame: _frame)
        tableView.delegate = self
        tableView.dataSource = self
        
        let indexPath = IndexPath(row: 9, section: 0)
        tableView.scrollToRow(at: indexPath, at: UITableViewScrollPosition.top, animated: true)
        
        let uialert = UIAlertController(title: "title", message: "hello,swfit", preferredStyle: UIAlertControllerStyle.alert)
        let okaction = UIAlertAction(title: "ok", style: UIAlertActionStyle.default, handler: nil)
        uialert.addAction(okaction)
        self.present(uialert, animated: true, completion: nil)
        
        self.view.addSubview(tableView)
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    
}


